package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.${className}Mapper;
import org.linlinjava.litemall.db.domain.${className};
import org.linlinjava.litemall.db.domain.${className}Example;
import org.linlinjava.litemall.db.domain.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDateTime;
import java.util.List;
<#import "CopyRight.ftl" as my>
<@my.CopyRight/>
@Service
@Transactional
public class ${className}Service {
    @Autowired
    private ${className}Mapper ${rootPath}Mapper;

    public ${className} findById(Integer id) {
        return ${rootPath}Mapper.selectByPrimaryKey(id);
    }


    public void add(${className} entity) {
        entity.setAddTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        ${rootPath}Mapper.insertSelective(entity);
    }

    public int updateById(${className} entity) {
        entity.setUpdateTime(LocalDateTime.now());
        return ${rootPath}Mapper.updateByPrimaryKeySelective(entity);
    }

    public List<${className}> querySelective(String name,Integer page, Integer size, String sort, String order) {
        ${className}Example example = new ${className}Example();
        ${className}Example.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return ${rootPath}Mapper.selectByExample(example);
    }

    public int count() {
        ${className}Example example = new ${className}Example();
        example.or().andDeletedEqualTo(false);

        return (int) ${rootPath}Mapper.countByExample(example);
    }

    public void deleteById(Integer id) {
        ${rootPath}Mapper.logicalDeleteByPrimaryKey(id);
    }

    public List<${className}> find${rootPath?cap_first}(String name) {
        ${className}Example example = new ${className}Example();
        example.or().andNameEqualTo(name).andDeletedEqualTo(false);
        return ${rootPath}Mapper.selectByExample(example);
    }


}
